﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
namespace BYManageInfoSystem
{
    public partial class Frm_FeeTTLoanAndFeeReg : Form
    {
        private DGBY.BLL.FactoryDataTab m_bllFactoryDataTab = new DGBY.BLL.FactoryDataTab();
        private DGBY.BLL.BiZhiTab m_bllBiZhiTab = new DGBY.BLL.BiZhiTab();
        private DGBY.BLL.FeeNotifyBankTab m_bllFeeNotifyBankTab = new DGBY.BLL.FeeNotifyBankTab();
        private DGBY.BLL.MDCountryTab m_bllMDCountryTab = new DGBY.BLL.MDCountryTab();
        private DGBY.BLL.BYDepartTab m_bllBYDepartTab = new DGBY.BLL.BYDepartTab();
        private DGBY.BLL.T_ACL_User m_bllT_ACL_User = new DGBY.BLL.T_ACL_User();
        private DGBY.BLL.FeeTTLoanAndFeeRegBasicInfoTab m_bllFeeTTLoanAndFeeRegBasicInfoTab = new DGBY.BLL.FeeTTLoanAndFeeRegBasicInfoTab();
        private DGBY.BLL.FeeTTLoanAndFeeRegDetailInfoTab m_bllFeeTTLoanAndFeeRegDetailInfoTab = new DGBY.BLL.FeeTTLoanAndFeeRegDetailInfoTab();
        private int m_nSelectFeeTTLoanAndFeeRegDetailInfoTabID = 0;
        UltraGridColumn m_ultraGridColumnBasicInfo = null;
        UltraGridColumn m_ultraGridColumnJiaoDan = null;
        UltraGridColumn m_ultraGridColumnShouKuan = null;
        UltraGridColumn m_ultraGridColumnKouFeeRecv = null;
        UltraGridColumn m_ultraGridColumnKouFeePay = null;
        private Dictionary<string, string> dicQueryCondition = new Dictionary<string, string>();

        string m_strQueryWhere = "";
        DataSet m_ds = new DataSet();
        private bool IsDecimal(string strInput)
        {
            bool bRet = Regex.IsMatch(strInput, @"([1-9]+[0-9]*(\.[0-9]+))|(0(\.[0-9]+))");
            return bRet;
        }

        private bool IsNum(string strInput)
        {
            bool bRet = Regex.IsMatch(strInput, @"^[1-9]+\d*$");
            return bRet;
        }

        private bool ValidDate(string strInput)
        {
            if (strInput.Length != 8)
                return false;
            else
                return IsNum(strInput);
        }

        public bool IsDecimal(ErrorProvider errorProvider1, TextBox TextBox)
        {
            if (TextBox.Text == "")
            {
                errorProvider1.SetError(TextBox, "内容不能为空！");
                return true;
            }
            else if (!IsDecimal(TextBox.Text) && !IsNum(TextBox.Text))
            {
                errorProvider1.SetError(TextBox, "必须输入数字");
                return true;
            }
            else
            {
                errorProvider1.SetError(TextBox, "");
                return false;
            }
        }

        public bool IsNum(ErrorProvider errorProvider1, TextBox TextBox)
        {
            if (TextBox.Text == "")
            {
                errorProvider1.SetError(TextBox, "内容不能为空！");
                return true;
            }
            else if (!IsNum(TextBox.Text))
            {
                errorProvider1.SetError(TextBox, "必须输入数字");
                return true;
            }
            else
            {
                errorProvider1.SetError(TextBox, "");
                return false;
            }
        }

        public bool IsNull(ErrorProvider errorProvider1, TextBox TextBox)
        {
            if (TextBox.Text == "")
            {
                errorProvider1.SetError(TextBox, "内容不能为空！");
                return true;
            }
            else
            {
                errorProvider1.SetError(TextBox, "");
                return false;
            }
        }
        public Frm_FeeTTLoanAndFeeReg()
        {
            InitializeComponent();
        }

        public void InitUltraWinGridSetting()
        {
            this.ultraGrid1.DisplayLayout.Override.RowSelectors = Infragistics.Win.DefaultableBoolean.True;
            this.ultraGrid1.DisplayLayout.Override.RowSelectorNumberStyle = Infragistics.Win.UltraWinGrid.RowSelectorNumberStyle.VisibleIndex;
            this.ultraGrid1.DisplayLayout.Override.RowSelectorWidth = 40;
        }

        public void LocalPrintPreveiw()
        {
            Infragistics.Shared.ResourceCustomizer rc = Infragistics.Win.Printing.Resources.Customizer;//Resources.Customizer;
            rc.SetCustomizedString("PrintPreview_DialogCaption", "打印预览");
            rc.SetCustomizedString("PrintPreview_Tool_Print", "打印(&P)");
            rc.SetCustomizedString("PrintPreview_Tool_ClosePreview", "关闭(&C)");
            rc.SetCustomizedString("PrintPreview_Tool_ContextMenuPreviewZoom", "显示比例");
            rc.SetCustomizedString("PrintPreview_Tool_Current_Page", "当前页");
            rc.SetCustomizedString("PrintPreview_Tool_Exit", "退出(&X)");
            rc.SetCustomizedString("PrintPreview_Tool_First_Page", "第一页");
            rc.SetCustomizedString("PrintPreview_Tool_Go_To", "跳至");
            rc.SetCustomizedString("PrintPreview_Tool_Last_Page", "最后一页");
            rc.SetCustomizedString("PrintPreview_Tool_Next_Page", "下一页");
            rc.SetCustomizedString("PrintPreview_Tool_Previous_Page", "前一页");
            rc.SetCustomizedString("PrintPreview_Tool_Next_View", "下一视图(&N)");
            rc.SetCustomizedString("PrintPreview_Tool_Previous_View", "前一视图(&P)");
            rc.SetCustomizedString("PrintPreview_Tool_Hand_Tool", "手型工具(&H)");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Setup", "页面设置(&U)");
            rc.SetCustomizedString("PrintPreview_Tool_Snapshot_Tool", "快照工具(&S)");
            rc.SetCustomizedString("PrintPreview_Tool_View", "视图(&V)");
            rc.SetCustomizedString("PrintPreview_Tool_Whole_Page", "合适页");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom", "缩放(&Z)");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_In", "放大");

            rc.SetCustomizedString("PrintPreview_Tool_Zoom_Out", "缩小");

            rc.SetCustomizedString("PrintPreview_ToolCategory_Context_Menus", "上下文菜单");
            rc.SetCustomizedString("PrintPreview_ToolCategory_File", "文件");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Menus", "菜单");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Tools", "工具栏");
            rc.SetCustomizedString("PrintPreview_ToolCategory_View", "视图");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Zoom_Mode", "缩放模式");

            rc.SetCustomizedString("PrintPreview_ToolTip_ClosePreview", "关闭");
            rc.SetCustomizedString("PrintPreview_ToolTip_Zoom", "缩放");
            rc.SetCustomizedString("StatusBar_Page_X_OF_X", "页:{0}/{1}");

            rc.SetCustomizedString("CustomizeImg_ToolBar_MenuBar", "菜单");
            rc.SetCustomizedString("CustomizeImg_ToolBar_Standard", "标准");
            rc.SetCustomizedString("CustomizeImg_ToolBar_View", "视图");
            rc.SetCustomizedString("PrintPreview_Tool_File", "文件(&F)");
            rc.SetCustomizedString("PrintPreview_Tool_Tools", "工具(&T)");
            rc.SetCustomizedString("PrintPreview_Tool_Dynamic_Zoom_Tool", "动态缩放工具(&D)");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_Out_Tool", "缩小工具");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_In_Tool", "放大工具");
            // rc.SetCustomizedString("PrintPreview_Tool_Page_Layout","菜单");
            rc.SetCustomizedString("PreviewRowColSelection_Cancel", "取消");
            rc.SetCustomizedString("PreviewRowColSelection_SelectedPages", "{0} x {1} 页");

            rc.SetCustomizedString("PreviewRowColSelection_Cancel", "取消");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Width", "页宽");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_MarginWidth", "文字宽度");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_PageWidth", "页宽");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_WholePage", "合适页");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Layout", "页面布局");
            rc.SetCustomizedString("PrintPreview_Tool_Margin_Width", "文字宽度");
            rc.SetCustomizedString("ContextMenuPreviewHand", "缩放视图");


            rc.SetCustomizedString("PrintPreview_Tool_Reduce_Page_Thumbnails", "缩小");
            rc.SetCustomizedString("PrintPreview_Tool_Show_Page_Numbers", "显示页号");
            rc.SetCustomizedString("PrintPreview_Tool_ContextMenuThumbnail", "缩略图");
            rc.SetCustomizedString("PrintPreview_Tool_Enlarge_Page_Thumbnails", "放大");
            rc.SetCustomizedString("PrintPreview_Tool_Thumbnails", "缩略图");
            rc.SetCustomizedString("PrintPreview_Tool_Continuous", "连续排序");
            //说明
            rc.SetCustomizedString("StatusBar_DynamicZoom_Instructions", "单击并拖动进行缩放操作");
            rc.SetCustomizedString("StatusBar_Page_X_OF_X", "当前页: {0} / {1}");
            rc.SetCustomizedString("StatusBar_SnapShot_Instructions", "单击并拖动,系统将选定矩型区域复制到剪帖板");
            rc.SetCustomizedString("StatusBar_ZoomIn_Instructions", "单击并拖动,系统将放大选定矩型区域");
            rc.SetCustomizedString("StatusBar_ZoomOut_Instructions", "单击并拖动,系统将缩小选定矩型区域");
            rc.SetCustomizedString("StatusBar_Hand_Instructions", "单击并拖动以便显示更多内容");
        }


        private void buttonContractNo_Click(object sender, EventArgs e)
        {
            Frn_TTWithContract frm_TTWithContract = new Frn_TTWithContract(m_nSelectFeeTTLoanAndFeeRegDetailInfoTabID);
            frm_TTWithContract.StartPosition = FormStartPosition.CenterScreen;
            frm_TTWithContract.ShowDialog();
        }

        private void InitCombox()
        {
            DataSet ds = m_bllFactoryDataTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxFac.DataSource = ds.Tables[0];

                this.comboBoxFac.ValueMember = "FactoryDataTabID";
                this.comboBoxFac.DisplayMember = "工厂名称";
                this.comboBoxFac.SelectedIndex = 0;
            }
            
            ds = m_bllBiZhiTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxBizhi.DataSource = ds.Tables[0];
                this.comboBoxBizhi.DisplayMember = "币制名称";
                this.comboBoxBizhi.ValueMember = "BiZhiTabID";
                this.comboBoxBizhi.SelectedIndex = 0;
            }

            ds = m_bllFeeNotifyBankTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxBank.DataSource = ds.Tables[0];
                this.comboBoxBank.DisplayMember = "通知银行简称";
                this.comboBoxBank.ValueMember = "FeeNotifyBankTabID";
                this.comboBoxBank.SelectedIndex = 0;
            }

            ds = m_bllMDCountryTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxNation.DataSource = ds.Tables[0];
                this.comboBoxNation.DisplayMember = "国家名称";
                this.comboBoxNation.ValueMember = "MDCountryTabID";
                this.comboBoxNation.SelectedIndex = 0;
            }
            ds = m_bllBYDepartTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxDep.DataSource = ds.Tables[0];
                this.comboBoxDep.DisplayMember = "部门名称";
                this.comboBoxDep.ValueMember = "BYDepartTabID";
                this.comboBoxDep.SelectedIndex = 0;
            }

            ds = m_bllT_ACL_User.GetList("Name != 'System1'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxDaiBiao.DataSource = ds.Tables[0];
                this.comboBoxDaiBiao.DisplayMember = "用户名称";
                this.comboBoxDaiBiao.ValueMember = "UserPasswordTabID";
                this.comboBoxDaiBiao.SelectedIndex = 0;
            }
            
        }

        private void Frm_FeeTTLoanAndFeeReg_Load(object sender, EventArgs e)
        {
            LocalPrintPreveiw();
            InitUltraWinGridSetting();
            InitCombox();
            InitQueryCondition();
        }

        private string MakeTTBianHao(string strNotifyBank)
        {
            string strYear = System.DateTime.Now.Year.ToString();
            DataSet ds = m_bllFeeTTLoanAndFeeRegBasicInfoTab.GetList("TTBianHao" + " LIKE " + "'%" + strNotifyBank + strYear + "%'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                List<int> nTTBianHaoLst = new List<int>();

                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    string strTTBianHao = ds.Tables[0].Rows[i]["TTBianHao"].ToString();
                    strTTBianHao = strTTBianHao.Substring(strYear.Length + strNotifyBank.Length, 4);
                    int nTTBianHao = Convert.ToInt32(strTTBianHao, 10);
                    nTTBianHaoLst.Add(nTTBianHao);
                }

                nTTBianHaoLst.Sort();
                int nNextBianHao = nTTBianHaoLst[nTTBianHaoLst.Count - 1] + 1;
                return strNotifyBank + strYear + nNextBianHao.ToString().PadLeft(4, '0');

            }
            else
            {
                return strNotifyBank + strYear + "0001";
            }

            
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            try
            {
                string strTTNo = this.textBoxTTNo.Text;
                DataSet ds = m_bllFeeTTLoanAndFeeRegBasicInfoTab.GetList("FeeTTLoanAndFeeRegBasicInfoTab.TTNo = '" + strTTNo + "'");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    DGBY.Model.FeeTTLoanAndFeeRegDetailInfoTab modelFeeTTLoanAndFeeRegDetailInfoTab = new DGBY.Model.FeeTTLoanAndFeeRegDetailInfoTab();

                    modelFeeTTLoanAndFeeRegDetailInfoTab.FeeTTLoanAndFeeRegBasicInfoTabID
                        = Convert.ToInt32(ds.Tables[0].Rows[0]["FeeTTLoanAndFeeRegBasicInfoTabID"].ToString(), 10);
                    ds = m_bllFeeTTLoanAndFeeRegDetailInfoTab.GetCount("FeeTTLoanAndFeeRegDetailInfoTab.FeeTTLoanAndFeeRegBasicInfoTabID = '" + modelFeeTTLoanAndFeeRegDetailInfoTab.FeeTTLoanAndFeeRegBasicInfoTabID.ToString() + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        modelFeeTTLoanAndFeeRegDetailInfoTab.JiaoDanTimes
                            = Convert.ToInt32(ds.Tables[0].Rows[0]["DetailRecCount"].ToString(), 10) + 1;
                        modelFeeTTLoanAndFeeRegDetailInfoTab.PayMoneyKouFeeDate = this.dateTimePickerPayKouFee.Value;
                        modelFeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyKouFeeDate = this.dateTimePickerRecvKouFee.Value;
                        modelFeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyDate = this.dateTimePickerRecvMoney.Value;
                        modelFeeTTLoanAndFeeRegDetailInfoTab.JiaoDanDate = this.dateTimePickerJiaoDanDate.Value;
                        modelFeeTTLoanAndFeeRegDetailInfoTab.OutGoodsDate = this.dateTimePickerOutGoods.Value;
                        m_bllFeeTTLoanAndFeeRegDetailInfoTab.Add(modelFeeTTLoanAndFeeRegDetailInfoTab);
                    }

                }
                else
                {
                    if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxNewTTJinE))
                        return;
                    if (ClassInputValid.IsNum(this.errorProvider1, this.textBoxModifyTimes))
                        return;
                    DGBY.Model.FeeTTLoanAndFeeRegBasicInfoTab modelFeeTTLoanAndFeeRegBasicInfoTab = new DGBY.Model.FeeTTLoanAndFeeRegBasicInfoTab();

                    modelFeeTTLoanAndFeeRegBasicInfoTab.TTBianHao = MakeTTBianHao(this.comboBoxBank.Text);
                    modelFeeTTLoanAndFeeRegBasicInfoTab.TTNo = this.textBoxTTNo.Text;
                    modelFeeTTLoanAndFeeRegBasicInfoTab.ModifyTimes = Convert.ToInt32(this.textBoxModifyTimes.Text, 10);
                    modelFeeTTLoanAndFeeRegBasicInfoTab.NewTTJinE = Convert.ToDecimal(this.textBoxNewTTJinE.Text);
                    modelFeeTTLoanAndFeeRegBasicInfoTab.NewTTBank = this.textBoxNewTTBank.Text;
                    modelFeeTTLoanAndFeeRegBasicInfoTab.NewTTMan = this.textBoxNewTTMan.Text;
                    modelFeeTTLoanAndFeeRegBasicInfoTab.IsJiaoDan = "否";
                    modelFeeTTLoanAndFeeRegBasicInfoTab.IsRecvMoney = "否";

                    if (this.comboBoxFac.SelectedValue != null)
                        modelFeeTTLoanAndFeeRegBasicInfoTab.FactoryDataTabID = Convert.ToInt32(this.comboBoxFac.SelectedValue.ToString(), 10);
                    if (this.comboBoxBizhi.SelectedValue != null)
                        modelFeeTTLoanAndFeeRegBasicInfoTab.BiZhiTabID = Convert.ToInt32(this.comboBoxBizhi.SelectedValue.ToString(), 10);
                    if (this.comboBoxBank.SelectedValue != null)
                        modelFeeTTLoanAndFeeRegBasicInfoTab.FeeNotifyBankTabID = Convert.ToInt32(this.comboBoxBank.SelectedValue.ToString(), 10);
                    if (this.comboBoxNation.SelectedValue != null)
                        modelFeeTTLoanAndFeeRegBasicInfoTab.MDCountryTabID = Convert.ToInt32(this.comboBoxNation.SelectedValue.ToString(), 10);
                    if (this.comboBoxDep.SelectedValue != null)
                        modelFeeTTLoanAndFeeRegBasicInfoTab.BYDepartTabID = Convert.ToInt32(this.comboBoxDep.SelectedValue.ToString(), 10);
                    if (this.comboBoxDaiBiao.SelectedValue != null)
                        modelFeeTTLoanAndFeeRegBasicInfoTab.UserPasswordTabID = Convert.ToInt32(this.comboBoxDaiBiao.SelectedValue.ToString(), 10);

                    modelFeeTTLoanAndFeeRegBasicInfoTab.NewTTDate = this.dateTimePickerNewTT.Value;
                    modelFeeTTLoanAndFeeRegBasicInfoTab.DeadLineShipDate = this.dateTimePickerDeadlineShip.Value;
                    modelFeeTTLoanAndFeeRegBasicInfoTab.DeadLineYaHuiDate = this.dateTimePickerDeadlineYaHui.Value;



                    int nReturnID = m_bllFeeTTLoanAndFeeRegBasicInfoTab.Add(modelFeeTTLoanAndFeeRegBasicInfoTab);
                    DGBY.Model.FeeTTLoanAndFeeRegDetailInfoTab modelFeeTTLoanAndFeeRegDetailInfoTab = new DGBY.Model.FeeTTLoanAndFeeRegDetailInfoTab();
                    modelFeeTTLoanAndFeeRegDetailInfoTab.FeeTTLoanAndFeeRegBasicInfoTabID = nReturnID;
                    modelFeeTTLoanAndFeeRegDetailInfoTab.JiaoDanTimes = 1;
                    modelFeeTTLoanAndFeeRegDetailInfoTab.PayMoneyKouFeeDate = this.dateTimePickerPayKouFee.Value;
                    modelFeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyKouFeeDate = this.dateTimePickerRecvKouFee.Value;
                    modelFeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyDate = this.dateTimePickerRecvMoney.Value.Date;
                    modelFeeTTLoanAndFeeRegDetailInfoTab.JiaoDanDate = this.dateTimePickerJiaoDanDate.Value;
                    modelFeeTTLoanAndFeeRegDetailInfoTab.OutGoodsDate = this.dateTimePickerOutGoods.Value;
                    m_bllFeeTTLoanAndFeeRegDetailInfoTab.Add(modelFeeTTLoanAndFeeRegDetailInfoTab);
                }


                ShowAllRecordInDataGridView("");
                //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        
        }
        void ConvertStringToMultiLineFirst()
        {
            foreach (UltraGridColumn column2 in ultraGrid1.DisplayLayout.Bands[0].Columns)
            {
                string str = column2.Header.Caption;
                if (str == "合同号")
                {
                    int nRowsCount = this.ultraGrid1.Rows.Count;
                    for (int i = 0; i < nRowsCount; i++)
                    {
                        string strSource = this.ultraGrid1.Rows[i].GetCellValue(column2).ToString();
                        ConvertStringToMultiLineSecond(strSource, i);
                    }
                }
            }
        }

        private void ConvertStringToMultiLineSecond(string strSource, int nRowIndex)
        {
            string str = strSource.ToString();
            string[] arr = str.Split(',');
            string tempa = "";
            int n = 0;
            foreach (string ch in arr)
            {

                if (ch != "")
                {
                    if (n == 0)
                        tempa += ch;
                    else
                        tempa += "\n" + ch;
                }

                n++;
            }

            this.ultraGrid1.Rows[nRowIndex].Cells["合同号"].Value = tempa;
        }
        private void ProcessMultiHeader()
        {
            int nSpanX = 0;
            int nOriginX = 0;
            int nOriginXTemp = 0;
            bool bSpan = false;
            bool bStart = false;
            foreach (UltraGridColumn column2 in this.ultraGrid1.DisplayLayout.Bands[0].Columns)
            {
                string str = column2.Header.Caption;
                if (str == "交单次数")
                {
                    bSpan = false;
                    bStart = true;
                    //break;
                }

                if (!bSpan)
                {
                    if (str == "基本资料")
                        continue;
                    if(bStart)
                    {
                        column2.RowLayoutColumnInfo.OriginY = 2;
                    }
                    else
                    {
                        column2.RowLayoutColumnInfo.SpanY = 4;
                        if (column2.Hidden == false)
                            nOriginXTemp += 2;
                    }

                }
                else
                {
                    nSpanX += 2;
                    column2.RowLayoutColumnInfo.OriginY = 2;
                }
                if (str == "收款情况")
                {
                    bSpan = true;
                    nOriginX = nOriginXTemp;
                }
            }

            m_ultraGridColumnBasicInfo = ultraGrid1.DisplayLayout.Bands[0].Columns.Add("基本资料", "基本资料");
            m_ultraGridColumnBasicInfo.CellActivation = Activation.Disabled;
            m_ultraGridColumnBasicInfo.SortIndicator = SortIndicator.Disabled;
            m_ultraGridColumnBasicInfo.RowLayoutColumnInfo.PreferredCellSize = new Size(1, 1);
            m_ultraGridColumnBasicInfo.RowLayoutColumnInfo.AllowLabelSizing = RowLayoutSizing.None;
            m_ultraGridColumnBasicInfo.RowLayoutColumnInfo.LabelPosition = LabelPosition.LabelOnly;
            m_ultraGridColumnBasicInfo.RowLayoutColumnInfo.OriginX = nOriginX;//有2个列隐藏了
            m_ultraGridColumnBasicInfo.RowLayoutColumnInfo.OriginY = 0;
            m_ultraGridColumnBasicInfo.RowLayoutColumnInfo.SpanX = nSpanX;
            m_ultraGridColumnBasicInfo.RowLayoutColumnInfo.SpanY = 2;

            nOriginX = nOriginX + nSpanX;
            nSpanX = 5 * 2;
            m_ultraGridColumnJiaoDan = ultraGrid1.DisplayLayout.Bands[0].Columns.Add("交单的情况", "交单的情况");
            m_ultraGridColumnJiaoDan.CellActivation = Activation.Disabled;
            m_ultraGridColumnJiaoDan.SortIndicator = SortIndicator.Disabled;
            m_ultraGridColumnJiaoDan.RowLayoutColumnInfo.PreferredCellSize = new Size(1, 1);
            m_ultraGridColumnJiaoDan.RowLayoutColumnInfo.AllowLabelSizing = RowLayoutSizing.None;
            m_ultraGridColumnJiaoDan.RowLayoutColumnInfo.LabelPosition = LabelPosition.LabelOnly;
            m_ultraGridColumnJiaoDan.RowLayoutColumnInfo.OriginX = nOriginX;//有2个列隐藏了
            m_ultraGridColumnJiaoDan.RowLayoutColumnInfo.OriginY = 0;
            m_ultraGridColumnJiaoDan.RowLayoutColumnInfo.SpanX = nSpanX;
            m_ultraGridColumnJiaoDan.RowLayoutColumnInfo.SpanY = 2;

            nOriginX = nOriginX + nSpanX;
            nSpanX = 5 * 2;
            m_ultraGridColumnShouKuan = ultraGrid1.DisplayLayout.Bands[0].Columns.Add("收款的情况", "收款的情况");
            m_ultraGridColumnShouKuan.CellActivation = Activation.Disabled;
            m_ultraGridColumnShouKuan.SortIndicator = SortIndicator.Disabled;
            m_ultraGridColumnShouKuan.RowLayoutColumnInfo.PreferredCellSize = new Size(1, 1);
            m_ultraGridColumnShouKuan.RowLayoutColumnInfo.AllowLabelSizing = RowLayoutSizing.None;
            m_ultraGridColumnShouKuan.RowLayoutColumnInfo.LabelPosition = LabelPosition.LabelOnly;
            m_ultraGridColumnShouKuan.RowLayoutColumnInfo.OriginX = nOriginX;//有2个列隐藏了
            m_ultraGridColumnShouKuan.RowLayoutColumnInfo.OriginY = 0;
            m_ultraGridColumnShouKuan.RowLayoutColumnInfo.SpanX = nSpanX;
            m_ultraGridColumnShouKuan.RowLayoutColumnInfo.SpanY = 2;

            nOriginX = nOriginX + nSpanX;
            nSpanX = 7 * 2;
            m_ultraGridColumnKouFeeRecv = ultraGrid1.DisplayLayout.Bands[0].Columns.Add("国内扣费(实收款)", "国内扣费(实收款)");
            m_ultraGridColumnKouFeeRecv.CellActivation = Activation.Disabled;
            m_ultraGridColumnKouFeeRecv.SortIndicator = SortIndicator.Disabled;
            m_ultraGridColumnKouFeeRecv.RowLayoutColumnInfo.PreferredCellSize = new Size(1, 1);
            m_ultraGridColumnKouFeeRecv.RowLayoutColumnInfo.AllowLabelSizing = RowLayoutSizing.None;
            m_ultraGridColumnKouFeeRecv.RowLayoutColumnInfo.LabelPosition = LabelPosition.LabelOnly;
            m_ultraGridColumnKouFeeRecv.RowLayoutColumnInfo.OriginX = nOriginX;//有2个列隐藏了
            m_ultraGridColumnKouFeeRecv.RowLayoutColumnInfo.OriginY = 0;
            m_ultraGridColumnKouFeeRecv.RowLayoutColumnInfo.SpanX = nSpanX;
            m_ultraGridColumnKouFeeRecv.RowLayoutColumnInfo.SpanY = 2;

            nOriginX = nOriginX + nSpanX;
            nSpanX = 6 * 2;
            m_ultraGridColumnKouFeePay = ultraGrid1.DisplayLayout.Bands[0].Columns.Add("国内扣费(实付款)", "国内扣费(实付款)");
            m_ultraGridColumnKouFeePay.CellActivation = Activation.Disabled;
            m_ultraGridColumnKouFeePay.SortIndicator = SortIndicator.Disabled;
            m_ultraGridColumnKouFeePay.RowLayoutColumnInfo.PreferredCellSize = new Size(1, 1);
            m_ultraGridColumnKouFeePay.RowLayoutColumnInfo.AllowLabelSizing = RowLayoutSizing.None;
            m_ultraGridColumnKouFeePay.RowLayoutColumnInfo.LabelPosition = LabelPosition.LabelOnly;
            m_ultraGridColumnKouFeePay.RowLayoutColumnInfo.OriginX = nOriginX;//有2个列隐藏了
            m_ultraGridColumnKouFeePay.RowLayoutColumnInfo.OriginY = 0;
            m_ultraGridColumnKouFeePay.RowLayoutColumnInfo.SpanX = nSpanX;
            m_ultraGridColumnKouFeePay.RowLayoutColumnInfo.SpanY = 2;
        }
        private void ShowAllRecordInDataGridView(string strWhere)
        {
            ultraGrid1.DataSource = null;
            m_ds = m_bllFeeTTLoanAndFeeRegDetailInfoTab.GetAllDetailInfoList(strWhere);

            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                ultraGrid1.DataSource = m_ds.Tables[0];
                this.ultraGrid1.DisplayLayout.Bands[0].Columns["合同号"].CellMultiLine = Infragistics.Win.DefaultableBoolean.True;
                //ConvertStringToMultiLineFirst();
                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                ProcessMultiHeader();
            }
        }

        private void buttonShowAll_Click(object sender, EventArgs e)
        {
            ShowAllRecordInDataGridView("");
        }

        private void ultraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {
            //ultraGrid1.DisplayLayout.Bands[0].UseRowLayout = true;
            //ultraGrid1.DisplayLayout.Override.RowSizing = RowSizing.AutoFree;
            //foreach (UltraGridColumn ugc in ultraGrid1.DisplayLayout.Bands[0].Columns)
            //    ugc.PerformAutoResize(PerformAutoSizeType.AllRowsInBand);

            ultraGrid1.DisplayLayout.Bands[0].UseRowLayout = true;
            ultraGrid1.DisplayLayout.Override.RowSizing = RowSizing.AutoFree;
            foreach (UltraGridColumn ugc in ultraGrid1.DisplayLayout.Bands[0].Columns)
                ugc.PerformAutoResize();
        }

        private void ultraGrid1_AfterCellActivate(object sender, EventArgs e)
        {
            string strFeeTTLoanAndFeeRegDetailInfoTabID = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells[0].Text;
            m_nSelectFeeTTLoanAndFeeRegDetailInfoTabID = Convert.ToInt32(strFeeTTLoanAndFeeRegDetailInfoTabID, 10);
            cellRegDetailInfoTabClick();
        }
       
        private void cellRegDetailInfoTabClick()
        {
            DGBY.Model.FeeTTLoanAndFeeRegDetailInfoTab modelFeeTTLoanAndFeeRegDetailInfoTab
                        = m_bllFeeTTLoanAndFeeRegDetailInfoTab.GetModel(m_nSelectFeeTTLoanAndFeeRegDetailInfoTabID);
            if (modelFeeTTLoanAndFeeRegDetailInfoTab != null)
            {
                DGBY.Model.FeeTTLoanAndFeeRegBasicInfoTab modelFeeTTLoanAndFeeRegBasicInfoTab
                    = m_bllFeeTTLoanAndFeeRegBasicInfoTab.GetModel(modelFeeTTLoanAndFeeRegDetailInfoTab.FeeTTLoanAndFeeRegBasicInfoTabID);
                this.textBoxTTNo.Text = modelFeeTTLoanAndFeeRegBasicInfoTab.TTNo;
                this.textBoxTTBianHao.Text = modelFeeTTLoanAndFeeRegBasicInfoTab.TTBianHao;
                this.textBoxModifyTimes.Text = modelFeeTTLoanAndFeeRegBasicInfoTab.ModifyTimes.ToString();
                this.textBoxNewTTJinE.Text = modelFeeTTLoanAndFeeRegBasicInfoTab.NewTTJinE.ToString();
                this.textBoxNewTTBank.Text = modelFeeTTLoanAndFeeRegBasicInfoTab.NewTTBank;
                this.textBoxNewTTMan.Text = modelFeeTTLoanAndFeeRegBasicInfoTab.NewTTMan;
                this.textBoxIsJiaoDan.Text = modelFeeTTLoanAndFeeRegBasicInfoTab.IsJiaoDan;
                this.textBoxIsRecvMoney.Text = modelFeeTTLoanAndFeeRegBasicInfoTab.IsRecvMoney;

                if (modelFeeTTLoanAndFeeRegBasicInfoTab.FactoryDataTabID != null)
                    this.comboBoxFac.SelectedValue = modelFeeTTLoanAndFeeRegBasicInfoTab.FactoryDataTabID;
                if (modelFeeTTLoanAndFeeRegBasicInfoTab.BiZhiTabID != null)
                    this.comboBoxBizhi.SelectedValue = modelFeeTTLoanAndFeeRegBasicInfoTab.BiZhiTabID;

                if (modelFeeTTLoanAndFeeRegBasicInfoTab.FeeNotifyBankTabID != null)
                    this.comboBoxBank.SelectedValue = modelFeeTTLoanAndFeeRegBasicInfoTab.FeeNotifyBankTabID;
                if (modelFeeTTLoanAndFeeRegBasicInfoTab.MDCountryTabID != null)
                    this.comboBoxNation.SelectedValue = modelFeeTTLoanAndFeeRegBasicInfoTab.MDCountryTabID;
                
                if (modelFeeTTLoanAndFeeRegBasicInfoTab.BYDepartTabID != null)
                    this.comboBoxDep.SelectedValue = modelFeeTTLoanAndFeeRegBasicInfoTab.BYDepartTabID;
                if (modelFeeTTLoanAndFeeRegBasicInfoTab.UserPasswordTabID != null)
                    this.comboBoxDaiBiao.SelectedValue = modelFeeTTLoanAndFeeRegBasicInfoTab.UserPasswordTabID;

                this.dateTimePickerNewTT.Value = (System.DateTime)modelFeeTTLoanAndFeeRegBasicInfoTab.NewTTDate;
                this.dateTimePickerDeadlineShip.Value = (System.DateTime)modelFeeTTLoanAndFeeRegBasicInfoTab.DeadLineShipDate;
                this.dateTimePickerDeadlineYaHui.Value = (System.DateTime)modelFeeTTLoanAndFeeRegBasicInfoTab.DeadLineYaHuiDate;

               
                
                
                this.dateTimePickerPayKouFee.Value = (System.DateTime)modelFeeTTLoanAndFeeRegDetailInfoTab.PayMoneyKouFeeDate;
                this.dateTimePickerRecvKouFee.Value = (System.DateTime)modelFeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyKouFeeDate;
                this.dateTimePickerRecvMoney.Value = (System.DateTime)modelFeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyDate;
                this.dateTimePickerJiaoDanDate.Value = (System.DateTime)modelFeeTTLoanAndFeeRegDetailInfoTab.OutGoodsDate;
                this.dateTimePickerOutGoods.Value = (System.DateTime)modelFeeTTLoanAndFeeRegDetailInfoTab.OutGoodsDate;
                
                this.textBoxJiaoDanJinE.Text = modelFeeTTLoanAndFeeRegDetailInfoTab.JiaoDanJinE.ToString();
                this.textBoxJiaoDanTimes.Text = modelFeeTTLoanAndFeeRegDetailInfoTab.JiaoDanTimes.ToString();

                this.textBoxRecvMoneyFact.Text = modelFeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyJinE.ToString();
                this.textBoxRecvMoneyHuiLv.Text = modelFeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyHuiLv.ToString();
                this.textBoxForeignKouFee.Text = modelFeeTTLoanAndFeeRegDetailInfoTab.ForeignKouFee.ToString();
                this.textBoxRecvMoneyRate.Text = modelFeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyCaseRate.ToString();


                this.textBoxKouFeeRecvKuaiDi.Text = modelFeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyKuaiDiFee.ToString();
                this.textBoxKouFeeRecvYiFu.Text = modelFeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyYiFuFee.ToString();
                this.textBoxKouFeeRecvNotify.Text = modelFeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyNotifyFee.ToString();
                this.textBoxKouFeeRecvOther.Text = modelFeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyOtherFee.ToString();
                this.textBoxKouFeeRecvTotal.Text = (modelFeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyKuaiDiFee
                    + modelFeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyYiFuFee
                    + modelFeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyNotifyFee
                    + modelFeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyOtherFee).ToString();
                this.textBoxKouFeeRecvRate.Text = modelFeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyKouFeeRate.ToString();

                this.textBoxKouFeePayKuaiDi.Text = modelFeeTTLoanAndFeeRegDetailInfoTab.PayMoneyKuaiDiFee.ToString();
                this.textBoxKouFeePayYiFu.Text = modelFeeTTLoanAndFeeRegDetailInfoTab.PayMoneyYiFuFee.ToString();
                this.textBoxKouFeePayNotify.Text = modelFeeTTLoanAndFeeRegDetailInfoTab.PayMoneyNotifyFee.ToString();
                this.textBoxKouFeePayOther.Text = modelFeeTTLoanAndFeeRegDetailInfoTab.PayMoneyOtherFee.ToString();
                this.textBoxKouFeePayTotal.Text = (modelFeeTTLoanAndFeeRegDetailInfoTab.PayMoneyKuaiDiFee
                    + modelFeeTTLoanAndFeeRegDetailInfoTab.PayMoneyYiFuFee
                    + modelFeeTTLoanAndFeeRegDetailInfoTab.PayMoneyOtherFee
                    + modelFeeTTLoanAndFeeRegDetailInfoTab.PayMoneyNotifyFee).ToString();
            }
        }

        private void buttonModify_Click(object sender, EventArgs e)
        {
            try
            {

                DGBY.Model.FeeTTLoanAndFeeRegDetailInfoTab modelFeeTTLoanAndFeeRegDetailInfoTabOld
           = m_bllFeeTTLoanAndFeeRegDetailInfoTab.GetModel(m_nSelectFeeTTLoanAndFeeRegDetailInfoTabID);

                if (modelFeeTTLoanAndFeeRegDetailInfoTabOld == null)
                {
                    MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxNewTTJinE))
                    return;
                if (ClassInputValid.IsNum(this.errorProvider1, this.textBoxModifyTimes))
                    return;

                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxJiaoDanJinE))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxRecvMoneyFact))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxRecvMoneyHuiLv))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxForeignKouFee))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxKouFeePayKuaiDi))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxKouFeePayYiFu))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxKouFeePayNotify))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxKouFeePayOther))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxKouFeeRecvNotify))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxKouFeeRecvOther))
                    return;

                DGBY.Model.FeeTTLoanAndFeeRegDetailInfoTab modelFeeTTLoanAndFeeRegDetailInfoTab = new DGBY.Model.FeeTTLoanAndFeeRegDetailInfoTab();

                modelFeeTTLoanAndFeeRegDetailInfoTab.FeeTTLoanAndFeeRegDetailInfoTabID = m_nSelectFeeTTLoanAndFeeRegDetailInfoTabID;
                modelFeeTTLoanAndFeeRegDetailInfoTab.FeeTTLoanAndFeeRegBasicInfoTabID = modelFeeTTLoanAndFeeRegDetailInfoTabOld.FeeTTLoanAndFeeRegBasicInfoTabID;

                modelFeeTTLoanAndFeeRegDetailInfoTab.JiaoDanJinE = Convert.ToDecimal(this.textBoxJiaoDanJinE.Text);
                modelFeeTTLoanAndFeeRegDetailInfoTab.JiaoDanTimes = modelFeeTTLoanAndFeeRegDetailInfoTabOld.JiaoDanTimes;
                modelFeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyJinE = Convert.ToDecimal(this.textBoxRecvMoneyFact.Text);
                modelFeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyHuiLv = Convert.ToDecimal(this.textBoxRecvMoneyHuiLv.Text);
                modelFeeTTLoanAndFeeRegDetailInfoTab.ForeignKouFee = Convert.ToDecimal(this.textBoxForeignKouFee.Text);

                modelFeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyCaseRate
                    = modelFeeTTLoanAndFeeRegDetailInfoTab.ForeignKouFee / modelFeeTTLoanAndFeeRegDetailInfoTab.JiaoDanJinE;



                modelFeeTTLoanAndFeeRegDetailInfoTab.PayMoneyKuaiDiFee = Convert.ToDecimal(this.textBoxKouFeePayKuaiDi.Text);
                modelFeeTTLoanAndFeeRegDetailInfoTab.PayMoneyYiFuFee = Convert.ToDecimal(this.textBoxKouFeePayYiFu.Text);
                modelFeeTTLoanAndFeeRegDetailInfoTab.PayMoneyNotifyFee = Convert.ToDecimal(this.textBoxKouFeePayNotify.Text);
                modelFeeTTLoanAndFeeRegDetailInfoTab.PayMoneyOtherFee = Convert.ToDecimal(this.textBoxKouFeePayOther.Text);

                modelFeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyKuaiDiFee = modelFeeTTLoanAndFeeRegDetailInfoTab.PayMoneyOtherFee;
                modelFeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyYiFuFee
                    = modelFeeTTLoanAndFeeRegDetailInfoTab.JiaoDanJinE * (Decimal)0.125 * modelFeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyHuiLv / 100;
                modelFeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyNotifyFee = Convert.ToDecimal(this.textBoxKouFeeRecvNotify.Text);
                modelFeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyOtherFee = Convert.ToDecimal(this.textBoxKouFeeRecvOther.Text);
                modelFeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyKouFeeRate
                    = (modelFeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyKuaiDiFee
                    + modelFeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyYiFuFee
                    + modelFeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyNotifyFee
                    + modelFeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyOtherFee)
                    / modelFeeTTLoanAndFeeRegDetailInfoTab.JiaoDanJinE
                    / modelFeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyHuiLv;


                modelFeeTTLoanAndFeeRegDetailInfoTab.PayMoneyKouFeeDate = this.dateTimePickerPayKouFee.Value;
                modelFeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyKouFeeDate = this.dateTimePickerRecvKouFee.Value;
                modelFeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyDate = this.dateTimePickerRecvMoney.Value.Date;
                modelFeeTTLoanAndFeeRegDetailInfoTab.JiaoDanDate = this.dateTimePickerJiaoDanDate.Value;
                modelFeeTTLoanAndFeeRegDetailInfoTab.OutGoodsDate = this.dateTimePickerOutGoods.Value;

                m_bllFeeTTLoanAndFeeRegDetailInfoTab.Update(modelFeeTTLoanAndFeeRegDetailInfoTab);




                DGBY.Model.FeeTTLoanAndFeeRegBasicInfoTab modelFeeTTLoanAndFeeRegBasicInfoTab
                        = m_bllFeeTTLoanAndFeeRegBasicInfoTab.GetModel(modelFeeTTLoanAndFeeRegDetailInfoTab.FeeTTLoanAndFeeRegBasicInfoTabID);

                //modelFeeTTLoanAndFeeRegBasicInfoTab.FeeTTLoanAndFeeRegBasicInfoTabID = 
                if (modelFeeTTLoanAndFeeRegBasicInfoTab.TTNo != this.textBoxTTNo.Text)
                {
                    modelFeeTTLoanAndFeeRegBasicInfoTab.TTNo = this.textBoxTTNo.Text;
                    modelFeeTTLoanAndFeeRegBasicInfoTab.TTBianHao = MakeTTBianHao(this.comboBoxBank.Text);
                }

                modelFeeTTLoanAndFeeRegBasicInfoTab.ModifyTimes = Convert.ToInt32(this.textBoxModifyTimes.Text, 10);
                modelFeeTTLoanAndFeeRegBasicInfoTab.NewTTJinE = Convert.ToDecimal(this.textBoxNewTTJinE.Text);
                modelFeeTTLoanAndFeeRegBasicInfoTab.NewTTBank = this.textBoxNewTTBank.Text;
                modelFeeTTLoanAndFeeRegBasicInfoTab.NewTTMan = this.textBoxNewTTMan.Text;
                modelFeeTTLoanAndFeeRegBasicInfoTab.IsJiaoDan
                    = modelFeeTTLoanAndFeeRegDetailInfoTab.JiaoDanJinE > 0 ? "是" : "否";
                modelFeeTTLoanAndFeeRegBasicInfoTab.IsRecvMoney
                    = modelFeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyJinE > 0 ? "是" : "否";

                if (this.comboBoxFac.SelectedValue != null)
                    modelFeeTTLoanAndFeeRegBasicInfoTab.FactoryDataTabID = Convert.ToInt32(this.comboBoxFac.SelectedValue.ToString(), 10);
                if (this.comboBoxBizhi.SelectedValue != null)
                    modelFeeTTLoanAndFeeRegBasicInfoTab.BiZhiTabID = Convert.ToInt32(this.comboBoxBizhi.SelectedValue.ToString(), 10);
                if (this.comboBoxBank.SelectedValue != null)
                    modelFeeTTLoanAndFeeRegBasicInfoTab.FeeNotifyBankTabID = Convert.ToInt32(this.comboBoxBank.SelectedValue.ToString(), 10);
                if (this.comboBoxNation.SelectedValue != null)
                    modelFeeTTLoanAndFeeRegBasicInfoTab.MDCountryTabID = Convert.ToInt32(this.comboBoxNation.SelectedValue.ToString(), 10);
                if (this.comboBoxDep.SelectedValue != null)
                    modelFeeTTLoanAndFeeRegBasicInfoTab.BYDepartTabID = Convert.ToInt32(this.comboBoxDep.SelectedValue.ToString(), 10);
                if (this.comboBoxDaiBiao.SelectedValue != null)
                    modelFeeTTLoanAndFeeRegBasicInfoTab.UserPasswordTabID = Convert.ToInt32(this.comboBoxDaiBiao.SelectedValue.ToString(), 10);

                modelFeeTTLoanAndFeeRegBasicInfoTab.NewTTDate = this.dateTimePickerNewTT.Value;
                modelFeeTTLoanAndFeeRegBasicInfoTab.DeadLineShipDate = this.dateTimePickerDeadlineShip.Value;
                modelFeeTTLoanAndFeeRegBasicInfoTab.DeadLineYaHuiDate = this.dateTimePickerDeadlineYaHui.Value;

                m_bllFeeTTLoanAndFeeRegBasicInfoTab.Update(modelFeeTTLoanAndFeeRegBasicInfoTab);

                ShowAllRecordInDataGridView("");
                MessageBox.Show("数据修改成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
           


        }
        //清空textbox
        private void ResetAllTextBox(Form form)
        {
            for (int i = 0; i < form.Controls.Count; i++)
            {
                if (form.Controls[i] is TextBox)
                {
                    (form.Controls[i] as TextBox).Text = "";
                }

                if (form.Controls[i] is GroupBox)
                {
                    for (int j = 0; j < form.Controls[i].Controls.Count; j++)
                    {
                        if (form.Controls[i].Controls[j] is TextBox)
                        {
                            (form.Controls[i].Controls[j] as TextBox).Text = "";
                        }
                    }
                }
            }
        }
        private void buttonDel_Click(object sender, EventArgs e)
        {
            DGBY.Model.FeeTTLoanAndFeeRegDetailInfoTab modelFeeTTLoanAndFeeRegDetailInfoTabOld
            = m_bllFeeTTLoanAndFeeRegDetailInfoTab.GetModel(m_nSelectFeeTTLoanAndFeeRegDetailInfoTabID);

            if (modelFeeTTLoanAndFeeRegDetailInfoTabOld == null)
            {
                MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (MessageBox.Show("确实要删除该条记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
            {
                m_bllFeeTTLoanAndFeeRegDetailInfoTab.Delete(m_nSelectFeeTTLoanAndFeeRegDetailInfoTabID);
                int nFeeTTLoanAndFeeRegBasicInfoTabID = modelFeeTTLoanAndFeeRegDetailInfoTabOld.FeeTTLoanAndFeeRegBasicInfoTabID;
                DataSet ds =m_bllFeeTTLoanAndFeeRegDetailInfoTab.GetAllDetailInfoList("FeeTTLoanAndFeeRegDetailInfoTab.FeeTTLoanAndFeeRegBasicInfoTabID = '" + nFeeTTLoanAndFeeRegBasicInfoTabID +"'");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                }
                else
                {
                    m_bllFeeTTLoanAndFeeRegBasicInfoTab.Delete(nFeeTTLoanAndFeeRegBasicInfoTabID);
                }

                ShowAllRecordInDataGridView("");
                MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                ResetAllTextBox(this);
                return;
            }
        }

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ultraGrid1.DataSource;

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                }
            }
        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {
            foreach (UltraGridColumn ugc in ultraGrid1.DisplayLayout.Bands[0].Columns)
                ugc.Width++;
            this.ultraGridPrintDocument1.Grid = this.ultraGrid1;
            this.ultraPrintPreviewDialog1.Document = this.ultraGridPrintDocument1;
            this.ultraPrintPreviewDialog1.ShowDialog(this);
        }
 

        private void InitQueryCondition()
        {
            this.comboBoxLogic.SelectedIndex = 0;
            this.comboBoxQueryCondition.SelectedIndex = 0;
            this.radioButtonValueIs.Select();
            dicQueryCondition.Add("部门", "BYDepartTab.DepartName");
            dicQueryCondition.Add("工厂名称", "FactoryDataTab.FactoryName");
            dicQueryCondition.Add("跟单员", "T_ACL_User.Name");
            dicQueryCondition.Add("通知银行", "FeeNotifyBankTab.FeeNotifyBankName");
            dicQueryCondition.Add("信用证编号", "FeeTTLoanAndFeeRegBasicInfoTab.TTBianHao");
            dicQueryCondition.Add("信用证号", "FeeTTLoanAndFeeRegBasicInfoTab.TTNo");
            dicQueryCondition.Add("开证金额", "FeeTTLoanAndFeeRegBasicInfoTab.NewTTJinE");
            dicQueryCondition.Add("币种", "BiZhiTab.BiZhiName");
            dicQueryCondition.Add("目的国", "MDCountryTab.MDCountryName");
                    
            dicQueryCondition.Add("开证行", "FeeTTLoanAndFeeRegBasicInfoTab.NewTTBank ");
            dicQueryCondition.Add("开证人", "FeeTTLoanAndFeeRegBasicInfoTab.NewTTMan");
            dicQueryCondition.Add("开证日期", "FeeTTLoanAndFeeRegBasicInfoTab.NewTTDate");
            dicQueryCondition.Add("最后船期", "FeeTTLoanAndFeeRegBasicInfoTab.DeadLineShipDate");
            dicQueryCondition.Add("最后押汇日", "FeeTTLoanAndFeeRegBasicInfoTab.DeadLineYaHuiDate");
            dicQueryCondition.Add("出货日", "FeeTTLoanAndFeeRegDetailInfoTab.OutGoodsDate");
            dicQueryCondition.Add("交单日", "FeeTTLoanAndFeeRegDetailInfoTab.JiaoDanDate");
            dicQueryCondition.Add("交单金额", "FeeTTLoanAndFeeRegDetailInfoTab.JiaoDanJinE");
            dicQueryCondition.Add("合同号", "dbo.f_FeeTTLoanContractTabCollectFun(FeeTTLoanAndFeeRegDetailInfoTab.FeeTTLoanAndFeeRegDetailInfoTabID)");
            dicQueryCondition.Add("收款日", "FeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyDate");
            dicQueryCondition.Add("实收款", "FeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyJinE");
        }



        private void StartQuery(bool bIsQueryInRes)
        {
            string strWhere = "";

            string strTemp = this.comboBoxQueryCondition.SelectedItem.ToString();
            if (this.radioButtonBetween.Checked)
            {

                if (strTemp == "信用证编号" || strTemp == "开证金额" || strTemp == "开证日期"
                    || strTemp == "最后船期" || strTemp == "最后押汇日" || strTemp == "出货日"
                    || strTemp == "交单日" || strTemp == "交单金额" || strTemp == "合同号"
                    || strTemp == "收款日" || strTemp == "实收款" )
                {
                    if (strTemp == "开证日期" || strTemp == "交单日" || strTemp == "收款日"
                        || strTemp == "最后船期" || strTemp == "最后押汇日" || strTemp == "出货日")
                    {
                        if (!ValidDate(this.textBoxFrom.Text) || !ValidDate(this.textBoxTo.Text))
                        {
                            MessageBox.Show("日期格式有误，正确的格式如 20100808 !", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }

                        if (this.textBoxFrom.Text != "" && this.textBoxTo.Text != "")
                        {
                            strWhere = "CONVERT(varchar(8), " + dicQueryCondition[strTemp] + ", 112) >= " + "'" + this.textBoxFrom.Text + "'" + " and CONVERT(varchar(8), " + dicQueryCondition[strTemp] + ", 112) <= " + "'" + this.textBoxTo.Text + "'";
                        }
                        else
                        {
                            MessageBox.Show("输入的查询条件有错误，请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                    }
                    else
                    {
                        if (this.textBoxFrom.Text != "" && this.textBoxTo.Text != "")
                        {
                            strWhere = dicQueryCondition[strTemp] + " BETWEEN " + "'" + this.textBoxFrom.Text + "'" + " AND " + "'" + this.textBoxTo.Text + "'";
                        }
                        else
                        {
                            MessageBox.Show("输入的查询条件有错误，请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                    }
                }
                else
                {
                    MessageBox.Show("输入的查询条件有错误，请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                if (strTemp == "开证日期" || strTemp == "交单日" || strTemp == "收款日"
                       || strTemp == "最后船期" || strTemp == "最后押汇日" || strTemp == "出货日")
                {
                    if (this.textBoxValueIs.Text != "")
                    {
                        if (!ValidDate(this.textBoxValueIs.Text))
                        {
                            MessageBox.Show("日期格式有误，正确的格式如 20100808 !", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                        strWhere = this.textBoxValueIs.Text + " LIKE CONVERT(varchar(8), " + dicQueryCondition[strTemp] + ", 112) ";
                    }
                    else
                    {
                        MessageBox.Show("输入的查询条件有错误，请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }
                else
                {
                    strWhere = dicQueryCondition[strTemp] + " LIKE " + "'%" + this.textBoxValueIs.Text + "%'";
                }
            }

            if (bIsQueryInRes == false)
            {
                m_strQueryWhere = strWhere;
            }
            else
            {
                if (m_strQueryWhere != "")
                {
                    if (this.comboBoxLogic.SelectedIndex == 0)
                    {
                        m_strQueryWhere = "(" + m_strQueryWhere + " AND " + strWhere + ")";
                    }
                    else if (this.comboBoxLogic.SelectedIndex == 1)
                    {
                        m_strQueryWhere = "(" + m_strQueryWhere + " OR " + strWhere + ")";
                    }
                    else
                    {
                        MessageBox.Show("查询条件有误，请选择\"并且\"或\"或者\"!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }

                }
                else
                {
                    MessageBox.Show("没有所基于的查询结果，请首先单据\"查询\"按键!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

            }

            ShowAllRecordInDataGridView(m_strQueryWhere);
        }

        private void buttonQueryFromResult_Click(object sender, EventArgs e)
        {
            StartQuery(true);
        }

        private void radioButtonValueIs_CheckedChanged(object sender, EventArgs e)
        {
            if (this.radioButtonValueIs.Checked)
            {
                this.textBoxFrom.Text = "";
                this.textBoxTo.Text = "";
            }
        }

        private void radioButtonBetween_CheckedChanged(object sender, EventArgs e)
        {
            if (this.radioButtonBetween.Checked)
                this.textBoxValueIs.Text = "";
        }

        private void buttonQuery_Click_1(object sender, EventArgs e)
        {
            StartQuery(false);
        }
    }
}



